package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserInfoParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.redis.service.IStatisticsVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IFansFollowService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.service.IVisitorService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStatisticsVORedisService statisticsVORedisService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IVisitorService visitorService;
    @Autowired
    private IFansFollowService fansFollowService;

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean result = userInfoService.findByPhone(phone);
        return !result;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        //校验手机号码
        userInfoService.sedVerifyCode(phone);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(RegistParamDomain params){
        userInfoService.regist(params);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password){
        Map<String, Object> data = userInfoService.login(username, password);
        return JsonResult.success(data);
    }

    @GetMapping("/users")
    public Object users(@UserInfoParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }


    @RequiredLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        //接收请求头中的token（判断用户是否已登录）
        // 没有：还未登录  有已经登录，重置时间即可
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserToken(token);
        return JsonResult.success(userInfo);
    }

    @GetMapping("/strategies/favor")
    public Object favor(String userId){
        return JsonResult.success(statisticsVORedisService.getFavorSids(userId));
    }

    @GetMapping("/collection/MyCollections")
    public Object MyCollections(@UserInfoParam UserInfo userInfo){
        List<Strategy> list = strategyService.getCollectionsById(userInfo.getId());
        return JsonResult.success(list);
    }

    @GetMapping("/get")
    public Object get(String id){
        return JsonResult.success(userInfoService.get(id));
    }

    @PostMapping("/visitor")
    public Object visitor(String ownerId, String currentUId){
        Visitor visitor = visitorService.visitor(ownerId, currentUId);
        Map<String, Object> map = new HashMap<>();
        List<UserInfo> visitors = visitor.getVisitors();
        if (visitors.size() <= 8) {
            visitors.subList(0, visitors.size());
        }else {
            visitors.subList(0, 8);
        }
        map.put("visitors",visitors);
        map.put("totalView",visitor.getTotalView());
        map.put("todayView",visitor.getTodayView());
        return JsonResult.success(map);
    }

    @PostMapping("/toFollowOtherUser")
    @RequiredLogin
    public Object toFollowOtherUser(String followId, @UserInfoParam UserInfo userInfo){
        fansFollowService.follow(followId, userInfo.getId());
        return JsonResult.success();
    }

    @GetMapping("/fansFollow")
    public Object fansFollow(String id){
        FansFollow list = fansFollowService.findByUid(id);
        List<UserInfo> userInfo = userInfoService.findById(list.getFollowId());
        Map<String, Object> map = new HashMap<>();
        map.put("follows", list.getFollowId().size());
        map.put("fans",list.getFansId().size());
        map.put("followsUsers",userInfo);
        return JsonResult.success(map);
    }

}
